#Metabolic COmparisons
setwd("C:/Users/peter/OneDrive/Life/Marine Biology/PhD Metabolic Drivers of Growth and Survival in Salmonids/Chapters/Papers/Chapter 1 Method Validation/Data/Repeats")

library(readxl)
library(tidyverse)

Theme = theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.title=element_text(size=20),axis.text=element_text(size=18))

#Load Data
Data = read_csv("Repeats20.csv")
#Data=subset(Data, Mrsq>0.8)
#Data = read_csv("D:/OneDrive/Life/Marine Biology/PhD Metabolic Drivers of Growth and Survival in Salmonids/Chapters/Data/Cantabria/2023/Data/Data Analysis/Repeats/REPresults2_Spain Raw Data")
Field = read_excel("C:/Users/peter/OneDrive/Life/Marine Biology/PhD Metabolic Drivers of Growth and Survival in Salmonids/Chapters/Papers/Chapter 1 Method Validation/Data/Data Analysis/Raw Data/Field Data.xlsx")
#Field = read_excel("C:/Users/peter/OneDrive/Life/Marine Biology/PhD Metabolic Drivers of Growth and Survival in Salmonids/Chapters/Papers/Chapter 1 Method Validation/Data/Data Analysis/Raw Data/Field Data.xlsx")


#Prep Data
Data$ID<- as.character(with(Data, paste0(Day,ScaleID)))
Data$MR=-Data$MR
Data$MR2=-Data$MR2
#remove outliers
Data=subset(Data, MR2<1)



########Within Field variation



###Coefficent of variation
Data2 = Data%>%
  dplyr::group_by(ID) %>%
  summarise(SD=sd(MR2),AV = mean(MR2))

Data2$Coef = Data2$SD/Data2$AV
Data2$Coef2 = Data2$SD/Data2$AV*100
mean(na.omit(Data2$Coef2))

#Data2=subset(Data2, Coef<1)
hist(Data2$Coef,breaks=20)
t.test(Data2$Coef)#does not overlap zero


Data=merge(Data,Data2, by=c("ID"))
Data=subset(Data)

#Change Data
Data$Day=as.character(Data$Day)
Data$Run=as.character(Data$Run)


###Better ICC
library(rptR)
r1 = rpt((MR) ~ log(Weight)+(Temp)+(1 | ID)+Run+Day, grname = c("ID"), data = Data, datatype = "Gaussian", 
         nboot = 1000, npermut = 0)#Main
print(r1)
summary(r1)
plot(r1)






###COmpare CoV and Repeatability of RMR



###Variation in RMR
setwd("C:/Users/peter/OneDrive/Life/Marine Biology/PhD Metabolic Drivers of Growth and Survival in Salmonids/Chapters/Papers/Chapter 1 Method Validation/Data/Repeats/Lab Repeats")
#setwd("C:/Users/peter/OneDrive/Life/Marine Biology/PhD Metabolic Drivers of Growth and Survival in Salmonids/Chapters/Papers/Chapter 1 Method Validation/Data/Repeats/Lab Repeats")
RA = read.csv("RMRRep_A")
RB = read.csv("RMRRep_B")
RC = read.csv("RMRRep_C")
RD = read.csv("RMRRep_D")
RE = read.csv("RMRRep_E")
RF = read.csv("RMRRep_F")
RG = read.csv("RMRRep_G")
RH = read.csv("RMRRep_H")
RI = read.csv("RMRRep_I")

RMRr = rbind(RA,RB,RC,RD,RE,RF,RG,RH,RI)
RMRr=RMRr[,-1]
RMRr = merge(RMRr, Field, id=c("id"))
RMRr = subset(RMRr, SP=="Trout")

RMRr2 = RMRr%>%
  group_by(ID) %>%
  summarise(SD=sd(RMRRep),AV = -mean(RMRRep))

RMRr2$Coef = RMRr2$SD/RMRr2$AV
RMRr2$Coef2 = RMRr2$SD/RMRr2$AV*100
mean(RMRr2$Coef2)
hist(RMRr2$Coef,breaks=10)
hist(Data2$Coef,breaks=10)
shapiro.test(Data2$Coef)
shapiro.test(RMRr2$Coef)

t.test(RMRr2$Coef, unique(Data2$Coef))
t.test(RMRr2$Coef, unique(Data$Coef)[seq(1:15)])
wilcox.test(RMRr2$Coef, unique(Data$Coef))
boxplot(RMRr2$Coef,unique(Data$Coef))


#Restructure
Var1 = dplyr::select(RMRr2, ID, Coef)
Var1$Method="RMR^L"
Var2 = dplyr::select(Data, ID, Coef)
Var2$Method="RMR^F"
Var=rbind(Var1,unique(Var2))
p=ggplot(Var, aes(y=Coef,x=Method))+geom_boxplot(outliers= FALSE)+geom_point(alpha=0.5,size=2)+theme_bw()+Theme+ylab("Coefficent of Variation")+ scale_x_discrete(
  labels = c(
    "RMR^L" = bquote(RMR[L]),
    "RMR^F" = bquote(RMR[F])
  ))+ylim(0,0.7)

ggsave("C:/Users/peter/OneDrive/Life/Marine Biology/PhD Metabolic Drivers of Growth and Survival in Salmonids/Chapters/Papers/Chapter 1 Method Validation/Data/Data Analysis/Plots/Coef of Var.tiff", plot = p, width = 4, height = 4, units = "in", dpi = 300)



#GLS
library(nlme)
gls_model <- gls(Coef ~ Method, data = Var, 
                 weights = varIdent(form = ~ 1 | Method))
summary(gls_model)

#Check
E1 = resid(gls_model, type ="pearson")
F1=fitted(gls_model)
eta=predict(gls_model, type="link")
plot(x=F1,y=E1)
abline(h=0,v=0,lty=2)
plot(x=eta,y=E1)
plot(x=(Var$Coef),y=E1)
boxplot(E1~Var$Method)
acf(resid(gls_model), main = "ACF of Residuals")
qqnorm(residuals(gls_model))
qqline(residuals(gls_model))
ranef(gls_model)
library(car)
vif(gls_model)


#Plot properly
Theme = theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.title=element_text(size=20),axis.text=element_text(size=18))

ggplot()+geom_boxplot(aes(y=RMRr2$Coef,x="RMR"))+geom_point(aes(y=RMRr2$Coef,x="RMR"))+geom_boxplot(aes(y=Data$Coef,x="RMRr"))+geom_point(aes(y=Data$Coef,x="RMRr"))+theme_bw()+Theme+theme(legend.position = 0)+
  ylab(expression(paste("Coefficent of variation")))+scale_x_discrete(labels=c("RMR",expression(RMR[F])))+xlab("Method")

#RPT
#RMRr = merge(RMRr, Field, by=c("ID"))
RMRr$RMR2 = -RMRr$RMRRep*RMRr$Weight

r2 = rpt((RMRRep) ~ (1 | ID), grname = c("ID"), data = RMRr, datatype = "Gaussian", 
         nboot = 1000, npermut = 0)
r2 = rpt((RMR2) ~ log(Weight)+(1 | ID), grname = c("ID"), data = RMRr, datatype = "Gaussian", 
         nboot = 1000, npermut = 0)
print(r2)
summary(r2)
plot(r2)
plot(r2, type = "permut", cex.main = 1)

#Vs SMR
RA = read.csv("SMRRep_A")
RB = read.csv("SMRRep_B")
RC = read.csv("SMRRep_C")
RD = read.csv("SMRRep_D")
RE = read.csv("SMRRep_E")
RF = read.csv("SMRRep_F")
RG = read.csv("SMRRep_G")
RH = read.csv("SMRRep_H")
RI = read.csv("SMRRep_I")

SMRr = rbind(RA,RB,RC,RD,RE,RF,RG,RH,RI)
SMRr=SMRr[,-1]
SMRr = merge(SMRr, Field, id=c("id"))
SMRr = subset(SMRr, SP=="Trout")
SMRr$SMR2 = -SMRr$SMRRep*SMRr$Weight

r2 = rpt((SMR2) ~ log(Weight)+(1 | ID), grname = c("ID"), data = SMRr, datatype = "Gaussian", 
         nboot = 1000, npermut = 0)
print(r2)
summary(r2)
plot(r2)

